package irri.soils.ssnm;

import irri.soils.ssnm.R;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ActivityMain extends SSNM implements View.OnClickListener{
    /** Called when the activity is first created. */
	TextView tv;
	private String TableName = "lang";
	private SQLiteDatabase myDB;
	private Intent intent;
	private Bundle b;
	private int DBversion = 2;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setCustomTitle();
        Button ok = (Button) findViewById(R.id.button1);
        ok.setOnClickListener(this);
        tv = (TextView) findViewById(R.id.textView1);
        b = this.getIntent().getExtras();
        myDB= null;
        
        try {
            myDB = this.openOrCreateDatabase("SSNM2", MODE_WORLD_WRITEABLE, null);
            myDB.execSQL("CREATE TABLE IF NOT EXISTS version (version INT(3));");
            
            Cursor c = myDB.rawQuery("SELECT COUNT(*) FROM version", null);
            if (c != null) {
                c.moveToFirst();
                if(c.getInt(0) == 1){
               	 Cursor c2 = myDB.rawQuery("SELECT * FROM version" , null);
               	 if(c2 != null){
               		 c2.moveToFirst();
               		if(c2.getInt(0)!=DBversion){
               		 myDB.execSQL("DROP TABLE version");
                   	 myDB.execSQL("DROP TABLE lang");
                   	 myDB.execSQL("DROP TABLE tblvar");
                   	 myDB.execSQL("DROP TABLE phil");
                   	 myDB.execSQL("DROP TABLE owner2");
                   	myDB.execSQL("CREATE TABLE version (version INT(3));");
                     ContentValues values = new ContentValues();
               		 values.put("version", DBversion);
               		 myDB.insert("version", null, values);
                    }
               	 }
               	 c2.close();
                }else{
                	ContentValues values = new ContentValues();
              		 values.put("version", DBversion);
              		 myDB.insert("version", null, values);
                }
                c.close();
            }
           }
           catch(Exception e) {
        	   System.out.println(e.toString());
            Log.e("Error", "Error", e);
           } finally {
            if (myDB != null)
             myDB.close();
           }
    }

	@Override
	public void onClick(View v) {
		myDB= null;
       
        try {
         myDB = this.openOrCreateDatabase("SSNM2", MODE_WORLD_WRITEABLE, null);
         myDB.execSQL("CREATE TABLE IF NOT EXISTS "
           + TableName
           + " (lang INT(3));");
         
         Cursor c = myDB.rawQuery("SELECT count(*) FROM " + TableName , null);
         if (c != null) {
             c.moveToFirst();
             intent = new Intent(
      				ActivityMain.this,
      				(c.getInt (0) == 0 ? ActivityA.class : ActivityMain3.class)
      			);
             if(c.getInt(0) == 1){
            	 Cursor c2 = myDB.rawQuery("SELECT * FROM " + TableName , null);
            	 if(c2 != null){
            		 c2.moveToFirst();
            		 Bundle b = new Bundle();
             		 b.putInt("lang", c2.getInt(0));
             		 intent.putExtras(b);
             		 c2.close();
            	 }
             }
             c.close();
         }
        }
        catch(Exception e) {
         Log.e("Error", "Error", e);
        } finally {
         if (myDB != null)
          myDB.close();
        }
        if(intent!=null){ 
        	intent.putExtras(b);
        	startActivity(intent);
        	finish();
        }
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}
	
}